<?php
/**
 * 分销佣金解冻
 * Date: 2020年3月10日
 * Time: 下午1:51:03
 */
namespace app\command;

use app\model\BbcModel\BusinessBudget;
use app\model\BbcModel\ChannelBusiness;
use app\model\BbcModel\Commission;
use app\model\BbcModel\MasterIncome;
use app\model\BbcModel\Member;
use app\model\BbcModel\OrderExtra;
use app\model\BbcModel\Store;
use app\model\BbcModel\StoreBudget;
use think\db;
use think\facade\Log;

class Distribution extends \think\console\Command
{
    protected function configure()
    {
        $this->setName('distribution')//命令
        ->setDescription('distribution');//描述
    }

    protected function execute(\think\console\Input $input, \think\console\Output $output)
    {
        $time=time();
        $list=Commission::instance()->lists([['thaw_time','<=',$time],['df','=',0],['status','=',0],['thaw_time','>',0]]);
        Commission::instance()->startTrans();
        try{
            foreach ($list as $v){
                $store=Store::instance()->info(['id'=>$v['mn_sid']]);
                if (!empty($store) && $v['store_money']>0) {
                    $wallet_accumulate_income=$store['wallet_accumulate_income']+$v['store_money'];
                    $wallet_income=$store['wallet_income']+$v['store_money'];
                    $frozen_income=$store['frozen_income']-$v['store_money'];
                    Store::instance()->edit(['id'=>$store['id'],'mid'=>$store['mid']],['wallet_accumulate_income'=>$wallet_accumulate_income,'wallet_income'=>$wallet_income,'frozen_income'=>$frozen_income]);
                    $store_add['money']=$v['store_money'];
                    $store_add['sn']=$v['sn'];
                    $store_add['goods_id']=$v['goods_id'];
                    $store_add['mid']=$store['mid'];
                    $store_add['mn_sid']=$v['mn_sid'];
                    $store_add['cid']=$store['cid'];
                    $store_add['cb_id']=$store['cb_id'];
                    $store_add['type']=1;
                    $store_add['cd']=$time;
                    $store_add['amount']=$wallet_income;
                    if($v['order_type']==1){
                        $store_add['remark']='商品利润';
                    }else{
                        $store_add['remark']='会员卡利润';
                    }
                    StoreBudget::instance()->add($store_add);
                }
                $business=ChannelBusiness::instance()->info(['id'=>$v['cb_id']]);
                if (!empty($business) && $v['shop_money']>0) {
                    $b_wallet_accumulate_income=$business['wallet_accumulate_income']+$v['shop_money'];
                    $b_wallet_income=$business['wallet_income']+$v['shop_money'];
                    $b_frozen_income=$business['frozen_income']-$v['shop_money'];
                    $mid=Member::instance()->where(['mobile'=>$business['mobile']])->value('mid');
                    ChannelBusiness::instance()->edit(['id'=>$business['id']],['wallet_accumulate_income'=>$b_wallet_accumulate_income,'wallet_income'=>$b_wallet_income,'frozen_income'=>$b_frozen_income]);
                    $add['money']=$v['shop_money'];
                    $add['amount']=$b_wallet_income;
                    $add['sn']=$v['sn'];
                    $add['goods_id']=$v['goods_id'];
                    $add['mid']=$mid;
                    $add['cid']=$business['cid'];
                    $add['cb_id']=$business['id'];
                    $add['type']=1;
                    $add['cd']=$time;
                    if($v['order_type']==1){
                        $add['remark']='商品利润';
                    }else{
                        $add['remark']='会员卡利润';
                    }
                    BusinessBudget::instance()->add($add);
                }
                if($v['master_money']>0){
                    $master=MasterIncome::instance()->info(['id'=>1]);
                    $m_wallet_accumulate_income=$master['wallet_accumulate_income']+$v['master_money'];
                    $m_frozen_income=$master['frozen_income']-$v['master_money'];
                    MasterIncome::instance()->edit(['id'=>$master['id']],['frozen_income'=>$m_frozen_income,'wallet_accumulate_income'=>$m_wallet_accumulate_income]);
                }
                if($v['order_type']==1){
                    OrderExtra::instance()->edit(['order_id'=>$v['order_id']],['is_commission'=>1]);
                }
                Commission::instance()->edit(['id'=>$v['id']],['status'=>1,'md'=>$time]);
            }
            Commission::instance()->commit();
            $output->writeln("success");
        }catch (\Exception $e) {
            Commission::instance()->rollback();
            echo $e->getMessage();
            return false;
        }

    }


}

